<template>
  <div class='xtx-goods-page' v-if="goods">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goods.categories[1].id}`">{{ goods.categories[1].name }}
        </XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goods.categories[0].id}`">{{ goods.categories[0].name }}
        </XtxBreadItem>
        <XtxBreadItem to="">{{ goods.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <goods-image :images="goods.mainPictures
          "></goods-image>
          <goods-sales></goods-sales>
        </div>
        <div class="spec">
          <goods-name :goods="goods"></goods-name>
          <!-- sku组件 -->
          <goods-sku :goods="goods" @change="change"></goods-sku>
          <!-- 数量选择组件 -->
          <xtx-numbox v-model="num" :max="goods.inventory">数量</xtx-numbox>
          <xtx-button type="middle" size="primary" style="margin-top:20px;">加入购物车</xtx-button>
        </div>

      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant :goodId="$route.params.id"/>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <goods-tabs></goods-tabs>
          <!-- 注意事项 -->
          <div class="goods-warn">
            <goods-warn></goods-warn>
          </div>
        </div>
        <!-- 24热榜+周榜 -->
        <div class="goods-aside">
          <goods-hot ></goods-hot>
          <goods-hot :type="2"></goods-hot>
        </div>

      </div>
    </div>
  </div>
</template>

<script setup>
import GoodsRelevant from './components/goods-relevant'
import { getProduct } from '@/api/product'
import { useRoute } from 'vue-router'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsTabs from './components/goods-tabs.vue'
import GoodsHot from './components/goods-hot.vue'
import GoodsWarn from './components/goods-warn.vue'
import { provide } from 'vue'
// 获取商品详情，渲染
const useGoods = () => { // 封装获取商品详情函数
  const goods = ref(null)
  const route = useRoute()
  watch(() => route.params.id, (nval) => {
    if (nval && `/product/${nval}` === route.path) {
      getProduct(nval).then(({ result }) => {
        // 使用v-if让组件重新销毁创建
        goods.value = null // 让商品数据先为空，当id变化时重新渲染组件
        nextTick(() => { // 不会立即执行，等页面重新变化再执行
          goods.value = result
        })
      })
    }
  }, {
    immediate: true
  })

  return goods
}

// 选择商品数量
const num = ref(1)
const change = (sku) => {
  // 修改商品的不同选中规格价格展示
  if (sku.skuId) {
    goods.value.price = sku.price
    goods.value.oldPrice = sku.oldPrice
    goods.value.inventory = sku.inventory
  }
}

const goods = useGoods()
provide('goods', goods)
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;

  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }

  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}

.goods-footer {
  display: flex;
  margin-top: 20px;

  .goods-article {
    width: 940px;
    margin-right: 20px;
  }

  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}

.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
